package com.springboot.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.commonutils.R;
import com.springboot.eduservice.entity.EduCourse;
import com.springboot.eduservice.entity.EduTeacher;
import com.springboot.eduservice.entity.vo.CourseInfoVo;
import com.springboot.eduservice.entity.vo.CoursePublishVo;
import com.springboot.eduservice.entity.vo.CourseQuery;
import com.springboot.eduservice.service.impl.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-05-12
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseServiceImpl eduCourseService;

    @Autowired
    private EduTeacherServiceImpl eduTeacherService;

    @Autowired
    private EduChapterServiceImpl eduChapterService;

    @Autowired
    private EduCourseDescriptionServiceImpl eduCourseDescriptionService;

    @Autowired
    private EduVideoServiceImpl eduVideoService;

    @PostMapping("addCourse")
    public R addCourse(@RequestBody CourseInfoVo courseInfoVo){
        String id = eduCourseService.addCourseDescription(courseInfoVo);
        return R.ok().data("id",id);
    }

    @GetMapping("getcourse/{id}")
    public R getcourseInfoVo(@PathVariable String id){
        CourseInfoVo courseByIId = eduCourseService.getCourseByIId(id);
        return R.ok().data("courseByIId",courseByIId);
    }

    @PostMapping("updatainfoCourse")
    public R updatainfoCourse(@RequestBody CourseInfoVo courseInfoVo){
        eduCourseService.updateCourse(courseInfoVo);
        return R.ok();
    }

    //根据课程id查询课程确认信息
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id) {
        CoursePublishVo coursePublishVo = eduCourseService.publishCourseInfo(id);
        return R.ok().data("publishCourse",coursePublishVo);
    }

    //课程最终发布
    //修改课程状态
    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");//设置课程发布状态
        eduCourseService.updateById(eduCourse);
        return R.ok();
    }

    //带分页查询的课程信息列表
    @ApiOperation(value = "带分页查询的课程信息列表")
    @PostMapping("selectCourseInfo/{current}/{limit}") //注意使用带条件的分页查询必须使用post请求
    public R selectCourseInfo(@PathVariable Long current, @PathVariable Long limit,
                              @RequestBody(required = false) CourseQuery courseQuery){
        Page<EduCourse> pageTeacher = new Page<>(current, limit);
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        String teacherId = courseQuery.getTeacherId();
        String title = courseQuery.getTitle();
        String id = null;
        if (!StringUtils.isEmpty(teacherId)){
            EduTeacher idByName = eduTeacherService.getIDByName(teacherId);
            id = idByName.getId();
        }

        if (!StringUtils.isEmpty(id)){
            queryWrapper.like("teacher_id",id);
        }
        if (!StringUtils.isEmpty(title)){
            queryWrapper.like("title",title);
        }

        //根据时间排序
        queryWrapper.orderByDesc("gmt_create");

        //调用方法实现条件查询分页
        eduCourseService.page(pageTeacher,queryWrapper);
        long total = pageTeacher.getTotal();    //总记录数
        List<EduCourse> records = pageTeacher.getRecords();    //数据集合
        return R.ok().data("total",total).data("records",records);
    }
    //删除课程信息
    @DeleteMapping("deteleCourseList/{id}")
    public R deteleCourseList(@PathVariable String id){
        eduCourseService.removeById(id);
        eduVideoService.removeVideoById(id);
        eduChapterService.deleteChapterById(id);
        eduCourseDescriptionService.removeById(id);
        return R.ok();
    }





}

